﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics.Contracts;
using System.ComponentModel.Composition;
using Kooboo.Collections;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(ISupportTicketRepository))]
    public partial class EntityRepositories:ISupportTicketRepository
    {

      
        public IQueryable<ISupportTicket> SupportTickets
        {
            get
            {
                return this.DataReaderContext.SupportTickets;
            }
        }

        public void Post(ISupportTicket supportTicket)
        {
            Contract.Requires(supportTicket != null);

            var item = new SupportTicket()
            {
                Body = supportTicket.Body,
                CreateDate = supportTicket.CreateDate,
                Email = supportTicket.Email,
                //ReplyToId = (supportTicket.Parent != null ? new Nullable<int>(supportTicket.Parent.Id) : null),
                Status = (int)supportTicket.Status,
                Subject = supportTicket.Subject,
                CustomerId = supportTicket.Customer.Id
            };
            this.DataWriterContext.SupportTickets.InsertOnSubmit(item);
            this.DataWriterContext.SubmitChanges();

            supportTicket.Id = item.Id;

            foreach (var i in supportTicket.SupportReplies)
            {
                var reply = new SupportReply
                {
                    Body = i.Body,
                    CreateDate = i.CreateDate,
                    Email = i.Email,
                    IsAdminReply = i.IsAdminReply,
                    Subject = i.Subject,
                    SupportTicket = item,
                    UserName = i.UserName
                };

                this.DataWriterContext.SupportReplies.InsertOnSubmit(reply);
                this.DataWriterContext.SubmitChanges();
                i.Id = reply.Id;
            }

        }

        public void Put(ISupportTicket supportTicket)
        {
            Contract.Requires(supportTicket != null);

            var item = this.DataWriterContext.SupportTickets
                .Where(i => i.Id == supportTicket.Id)
                .FirstOrDefault();

            if (item == null)
            {
                throw new KeyNotFoundException("ShopSetting: " + supportTicket.Id);

            }


            item.Body = supportTicket.Body;
            item.CreateDate = supportTicket.CreateDate;
            item.Email = supportTicket.Email;
            
            item.Status = (int)supportTicket.Status;
            item.Subject = supportTicket.Subject;
            item.CustomerId = supportTicket.Customer.Id;


            var replies = this.DataWriterContext.SupportReplies.Where(i => i.SupportTicketId == item.Id)
                .ToList();

            var result = replies.Compare(supportTicket.SupportReplies,(i1,i2)=>i1.Id == i2.Id,(i1,i2)=>
            {
                i1.Body = i2.Body;
                i1.CreateDate = i2.CreateDate;
                i1.Email = i2.Email;
                i1.IsAdminReply = i2.IsAdminReply;
                i1.Subject = i2.Subject;
                i1.UserName = i2.UserName;
                
            });

            foreach (var i in result.Added)
            {
                var reply = new SupportReply
                {
                    Body = i.Body,
                    CreateDate = i.CreateDate,
                    Email = i.Email,
                    IsAdminReply = i.IsAdminReply,
                    Subject = i.Subject,
                    SupportTicket = item,
                    UserName = i.UserName
                };

                this.DataWriterContext.SupportReplies.InsertOnSubmit(reply);
                this.DataWriterContext.SubmitChanges();
                i.Id = reply.Id;
            }

            foreach (var i in result.Deleted)
            {
                this.DataWriterContext.SupportReplies.DeleteOnSubmit((SupportReply)i);
            }
            

            this.DataWriterContext.SubmitChanges();
        }

        public void Delete(ISupportTicket supportTicket)
        {
            if (supportTicket != null)
            {
                var item = this.DataWriterContext.SupportTickets
                .Where(i => i.Id == supportTicket.Id)
                .FirstOrDefault();

                if (item != null)
                {
                    var replies = this.DataWriterContext.SupportReplies.Where(i => i.SupportTicketId == item.Id);

                    this.DataWriterContext.SupportReplies.DeleteAllOnSubmit(replies);

                    this.DataWriterContext.SupportTickets.DeleteOnSubmit(item);

                    this.DataWriterContext.SubmitChanges();
                }
            }
        }
    }
}
